import React from 'react'
import { hasToken } from '@/utils/storage'
import { Route, Redirect, useLocation } from 'react-router-dom'

export default function PrivateRoute({ children, component: Component, ...rest }) {
  const location = useLocation()
  return (
    <Route
      {...rest}
      render={
        () => {
          if (hasToken()) {
            return children ? children : <Component></Component>
          } else {
            return <Redirect to={
              {
                pathname: '/login',
                state: {
                  from: location.pathname
                }
              }
            }></Redirect>
          }
        }
      }
    ></Route>
  )
}
